package org.gw.building.ctrl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.gw.building.model.Tzgg;
import org.gw.building.model.base.TzggFj;
import org.gw.building.repositories.TzggRepository;
import org.gw.ylc.base.context.RequestContextHolder;
import org.gw.ylc.base.model.UserInfo;
import org.gw.ylc.base.util.PermissionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 通知公告管理
    * @ClassName: TzggCtrl
    * @author liuyong
    * @date 2017年7月31日
 */
@RestController
@RequestMapping("tzgggl")
public class TzggCtrl {
	@Autowired
	private TzggRepository tzggRepository;
	
	@RequestMapping(value = "/list")
    public Page<Tzgg> queryPage(String id, String bt, String fbr, String fbzt) {
		return tzggRepository.queryPage(id,PermissionUtil.toLikeString(bt), PermissionUtil.toLikeString(fbr), fbzt, RequestContextHolder.get().getPageRequest());
	}
	
	@RequestMapping(value = "/queryTzgg")
	public Tzgg queryTzgg(String id){
		List<TzggFj> fjList = tzggRepository.findFjByOwner(id);
		Tzgg entity = tzggRepository.findById(id);
		if(null != entity){
			entity.setFjList(fjList);
		}
        return entity;
    }
	
	@RequestMapping("addTzgg")
    @Transactional
    public Object addTzgg(Tzgg entity) throws UnsupportedEncodingException {
		Tzgg t = tzggRepository.findById(entity.getId());
		UserInfo userInfo = RequestContextHolder.get().getUser();
		String user = userInfo.getName()+"("+userInfo.getUid()+")";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sj = sdf.format(new Date());
		String content = entity.getNr();
		if (!StringUtils.isEmpty(content)) {
			content = URLDecoder.decode(content, "UTF-8");
		}
		entity.setNr(content);
		if(null == t){
			entity.setCjr(user);
			entity.setCjsj(sj);
		}
		entity.setXgr(user);
		entity.setXgsj(sj);
		entity.setFbr(user);
		
		return tzggRepository.save(entity);
	}
	
	@RequestMapping("deleteTzzg")
	public Object deleteTzzg(String id) {
		tzggRepository.delete(id);
        return id;
    }
	
	@RequestMapping("deleteAllTzzg")
	public Object deleteAllTzzg(String result) {
		String[] ids = result.split(",");
		for(String id : ids){
			tzggRepository.delete(id);
		}
		return null;
	}
	
	@RequestMapping("publishTzzg")
	public Object publishTzzg(String id,String fbzt){
		Tzgg t = tzggRepository.findById(id);
		if(null != t){
			UserInfo userInfo = RequestContextHolder.get().getUser();
			String user = userInfo.getName()+"("+userInfo.getUid()+")";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String sj = sdf.format(new Date());
			t.setFbzt(fbzt);
			t.setXgr(user);
			t.setXgsj(sj);
			tzggRepository.save(t);
		}
		return id;
	}
}
